<?php

namespace C\M;

use C\L\Model;
use Phalcon\Mvc\Model\Resultset\Simple as Resultset;

class CouponReceive extends Model
{
    public function initialize()
    {
        $this->hasOne(
            'coupon_id',
            Coupons::class,
            'id',
            [
            "alias" => 'Coupons',
            ]
        );
    }

    public function beforeValidationOnCreate()
    {
        $this->addtime = $this->uptime = time();
        return true;
    }

    public function beforeValidationOnUpdate()
    {
        $this->uptime = time();
        return true;
    }

    public function afterCreate()
    {
        $coupon = $this->getCoupons();

      // 发送用户消息
        $this->di['s_message']->sendMessageToUser(
            $this->uid,
            'coupon_received',
            [
            '__amount__' => $coupon->coupon_value,
            '__period__' => $coupon->effective_time,
            '__time__'   => date('Y-m-d H:i:s', $this->addtime),
            ],
        );
    }

    public function canUse($userId, $amount)
    {
        if ($this->uid != $userId) {
            return false;
        }

        if ($this->status != 'N') {
            return false;
        }

        $now = time();
        if ($this->stime > $now) {
            return false;
        }

        if ($this->etime < $now) {
            return false;
        }

        if ($this->coupons->use_condition > $amount) {
            return false;
        }

        return true;
    }

    public function use()
    {
        $this->status = 'Y';
        return $this->save();
    }

    public static function availableCoupons($userId, $amount)
    {
        $now = time();
        $sql = "" .
        "SELECT" .
        " coupon_receive.*, coupons.coupon_value, coupons.use_condition, coupons.use_condition_msg" .
        " FROM coupon_receive" .
        " JOIN coupons ON coupons.id = coupon_receive.coupon_id" .
        " WHERE coupon_receive.uid = " . $userId .
        " AND (coupon_receive.status = 'N'" .
        "  AND coupon_receive.stime <= " . $now .
        "  AND coupon_receive.etime >= " . $now .
        "  AND coupons.use_condition <= " . $amount .
        " )";


        $couponReceive = new CouponReceive();

        $result = new Resultset(
            null,
            $couponReceive,
            $couponReceive->getReadConnection()->query($sql)
        );
        return $result->toArray();
    }

    public static function unavailableCoupons($userId, $amount)
    {
        $now = time();
        $sql = "" .
        "SELECT" .
        " coupon_receive.*, coupons.coupon_value, coupons.use_condition, coupons.use_condition_msg" .
        " FROM coupon_receive" .
        " JOIN coupons ON coupons.id = coupon_receive.coupon_id" .
        " WHERE coupon_receive.uid = " . $userId .
        " AND (coupon_receive.status != 'N'" .
        "   OR coupon_receive.stime > " . $now .
        "   OR coupon_receive.etime < " . $now .
        "   OR coupons.use_condition > " . $amount .
        " )";

        $couponReceive = new CouponReceive();

        $result = new Resultset(
            null,
            $couponReceive,
            $couponReceive->getReadConnection()->query($sql)
        );
        return $result->toArray();
    }

    public static function availableCouponsCount($userId, $amount)
    {
        $now = time();
        $sql = "" .
        "SELECT COUNT(*) as count" .
        " FROM coupon_receive" .
        " JOIN coupons ON coupons.id = coupon_receive.coupon_id" .
        " WHERE coupon_receive.uid = " . $userId .
        " AND (coupon_receive.status = 'N'" .
        "  AND coupon_receive.stime <= " . $now .
        "  AND coupon_receive.etime >= " . $now .
        "  AND coupons.use_condition <= " . $amount .
        " )";

        $couponReceive = new CouponReceive();

        $result = new Resultset(
            null,
            $couponReceive,
            $couponReceive->getReadConnection()->query($sql)
        );
        return $result->toArray()[0]['count'];
    }
}
